package cn.kgc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.kgc.base.BaseInfoProperties;
import cn.kgc.base.SystemConstant;
import cn.kgc.entity.Interview;
import cn.kgc.form.InterviewAddForm;
import cn.kgc.mapper.InterviewMapper;
import cn.kgc.service.InterviewService;
import cn.kgc.utils.RedisUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;


/**
 * @author YC
 */
@Slf4j
@Service("interviewService")
public class InterviewServiceImpl extends ServiceImpl<InterviewMapper, Interview> implements InterviewService {

    @Resource
    private RedisUtil redisUtil;

    @Override
    public boolean addInterview(InterviewAddForm form) {
        Interview interview = BeanUtil.copyProperties(form, Interview.class);
        // 面试邀约的状态默认为等待候选人接受
        interview.setStatus(SystemConstant.INTERVIEW_STATUS_WAIT_ACCEPT);
        // 统计HR最近7天已经邀约的人数
        String key1 = BaseInfoProperties.REDIS_INTERVIEW_HR_INVITE_COUNT + ":" + interview.getHrUserId();
        // 求职者最近7天被邀约的次数
        String key2 = BaseInfoProperties.REDIS_INTERVIEW_JOBSEEKEY_WES_INVITE_COUNT + ":" + interview.getCandUserId();
        redisUtil.incrementFirstExpire(key1, 7, TimeUnit.DAYS);
        redisUtil.incrementFirstExpire(key2, 7, TimeUnit.DAYS);
        return baseMapper.insert(interview) > 0;
    }

    @Override
    public boolean acceptInterview(String interviewId, String userId) {
        Interview interview = baseMapper.selectById(interviewId);
        // 邀约不存在或者邀约状态不是等待接受返回失败
        if (interview.getStatus().equals(SystemConstant.INTERVIEW_STATUS_WAIT_ACCEPT)) {
            return false;
        }
        // 面试邀约时间大于当前时间返回失败
        if (interview.getInterviewTime().getTime() > System.currentTimeMillis()) {
            return false;
        }
        // 修改面试邀约状态为接受
        interview.setStatus(SystemConstant.INTERVIEW_STATUS_ACCEPT);
        // 被邀约人的邀约次数加1
        redisUtil.increment(BaseInfoProperties.REDIS_INTERVIEW_JOBSEEKEY_WES_INVITE_COUNT + ":" + userId);
        return updateById(interview);
    }

    @Override
    public boolean refusedInterview(String interviewId, String userId) {
        Interview interview = baseMapper.selectById(interviewId);
        // 邀约不存在或者邀约状态不是等待接受返回失败
        if (interview.getStatus().equals(SystemConstant.INTERVIEW_STATUS_WAIT_ACCEPT)) {
            return false;
        }
        // 面试邀约时间大于当前时间返回失败
        if (interview.getInterviewTime().getTime() > System.currentTimeMillis()) {
            return false;
        }
        // 修改面试邀约状态为拒绝
        interview.setStatus(SystemConstant.INTERVIEW_STATUS_REFUSED);
        // 求职者最近7天拒绝邀约的次数
        String key = BaseInfoProperties.REDIS_INTERVIEW_JOBSEEKEY_REFESHED_COUNT + ":" + interview.getCandUserId();
        redisUtil.incrementFirstExpire(key, 7, TimeUnit.DAYS);
        return updateById(interview);
    }

    @Override
    public boolean hrCancelInterview(String interviewId, String hrId) {
        Interview interview = baseMapper.selectById(interviewId);
        // 邀约状态不是求职者拒绝邀约 返回失败
        if (interview.getStatus().equals(SystemConstant.INTERVIEW_STATUS_REFUSED)) {
            return false;
        }
        // 求职者最近7天接受邀约的次数 - 1
        String key1 = BaseInfoProperties.REDIS_INTERVIEW_JOBSEEKEY_WES_INVITE_COUNT + ":" + interview.getCandUserId();
        if (interview.getStatus().equals(SystemConstant.INTERVIEW_STATUS_ACCEPT)) {
            redisUtil.decrement(key1);
        }
        // HR最近7天取消邀约的次数
        String key2 = BaseInfoProperties.REDIS_INTERVIEW_HR_CANCEL_COUNT + ":" + interview.getHrUserId();
        redisUtil.incrementFirstExpire(key2, 7, TimeUnit.DAYS);
        // 修改面试邀约状态为HR取消面试邀约
        interview.setStatus(SystemConstant.INTERVIEW_STATUS_HR_CANCEL);
        return updateById(interview);
    }

}
